<?php

namespace app\admin\controller;

use app\admin\model\Banner as BannerModel;

class Banner extends AdminBase
{
    public function bannerList()
    {
        $data = model('banner')->getBanners(10);

        $this->assign("data", $data['data']);
        $this->assign("page", $data['page']);
        $this->assign("keywords", input('param.keywords', ''));
        return $this->fetch();
    }

    /**
     * @Title: add
     * @Description: Banner添加
     * @return mixed|void
     * @Author: 李军伟
     */
    public function add()
    {
        if (request()->isPost()) {
            $input = input();
            $app_url = $this->comCheck($input);
            $insert_data = array(
                'location' => $input['location'],
                'language' => $input['language'],
                'app_url' => json_encode($app_url),
                'banner_image' => $input['banner_image'],
                'created_time' => time()
            );
            $res = model('banner')->insert($insert_data);
            if ($res > 0) {
                $this->success('添加成功', 'bannerList');
            } else {
                $this->error('添加失败，请重试');
            }
        } else {
            //加载页面
            return $this->fetch();
        }
    }

    /**
     * @Title: del
     * @Description: banner删除
     * @return mixed
     * @Author: 李军伟
     */
    public function del()
    {
        if (request()->isPost()) {
            $article_id = input("param.id");
            $info = model('banner')->delWen($article_id);
            if ($info > 0) {
                $this->success('删除成功', 'getList');
            } else {
                $this->error('删除失败，请重试');
            }
        }
    }

    /**
     * @Title: update
     * @Description: banner更新
     * @return mixed|void
     * @Author: 李军伟
     */
    public function update()
    {
        if (request()->isPost()) {
            $input = input();
            $this->comCheck($input);
            $list = array(
                'id' => $input['id'],
                'location' => $input['location'],
                'language' => $input['language'],
                'banner_image' => $input['banner_image'],
                'updated_time' => time()
            );
            $res = model('banner')->editBanner($list);
            if ($res > 0) {
                $this->success('更新成功', 'bannerList');
            } else {
                $this->error('更新失败，请重试');
            }
        } else {
            // 加载页面
            $input = input();
            $where = ['id' => $input['id']];
            $info = (new BannerModel())->getOne($where);
            // 获取分类列表
            $list = [
                ['id' => 1, 'name' => '首页'],
                ['id' => 2, 'name' => '市场'],
                ['id' => 3, 'name' => '我的'],
                ['id' => 4, 'name' => '分享海报'],
            ];
            $this->assign("list", $list);
            $this->assign("data", $info);
            return $this->fetch();
        }
    }

    public function comCheck($input)
    {
        if (empty($input['location'])) {
            $this->error('位置不可为空');
        }
        if (!is_numeric($input['location'])) {
            $this->error('位置必须为数字');
        }
        if (empty($input['banner_image'])) {
            $this->error('图片不可为空');
        }
        if (!isset($input['language'])) {
            $this->error('语言不可为空');
        }
        // APP跳转
        $app_url = [
            'type' => $input['app_type'],
            'url' => $input['app_type'] == 1 ? $input['app_url1'] : $input['app_url2'],
            'params' => [
                'id' => $input['app_params']
            ],
        ];
        return $app_url;
    }
}
